package com.lsz.flight_system.controller;

import com.lsz.flight_system.entity.Admin;
import com.lsz.flight_system.entity.PlaneMessage;
import com.lsz.flight_system.service.AdminService;
import com.lsz.flight_system.service.PlaneMessageService;
import com.lsz.flight_system.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @Autowired
    PlaneMessageService planeMessageService;

    private PlaneMessage message = new PlaneMessage();
    private Admin adminOld = new Admin();

    @GetMapping("/list")
    public Result findAllAdmin(@RequestParam(value = "username",required = false) String username){
        Map<String,Object> queryMap = new HashMap<>();

        queryMap.put("username",username);

        return Result.ok(adminService.findAllAdmin(queryMap));
    }

    @GetMapping("/delAdmin/{username}")
    public Result delAdminByUsername(@PathVariable String username){
        if(adminService.delAdminByUsername(username)){
            return Result.ok();
        }
        return Result.fail("删除失败");
    }
    @GetMapping("/saveUpdateAdminMessage/{username}")
    public Result saveUpdateAdminMessage(@PathVariable String username){
        adminOld = adminService.getAdminByUsername(username);
        return Result.ok();
    }
    @GetMapping("/addAdmin")
    public  Result addAdminMessage(@RequestParam String username,
                                   @RequestParam String password){
        if (username.length()<6){
            return Result.fail("用户名长度不能小于6位");
        }
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        if (!adminService.addAdmin(admin)){
            return Result.fail("该用户名存在");
        }
        return Result.ok();
    }

    @GetMapping("/editAdminMessage")
    public Result editAdminMessage(@RequestParam String username,
                                   @RequestParam String password){

        if (username.length()<6){
            return Result.fail("用户名长度不能小于6位");
        }
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);

        adminService.delAdminByUsername(adminOld.getUsername());
//        System.out.println("测试——————"+adminOld);
        if (!adminService.addAdmin(admin)){
            adminService.addAdmin(adminOld);
            return Result.fail("该用户名存在");
        }
        return Result.ok();
    }

    @GetMapping("/delPlaneMessage/{planeId}")
    public Result delPlaneMessageByPlaneId(@PathVariable("planeId") String planeId){
        if (planeMessageService.delPlaneMessageById(planeId)){
            return Result.ok();
        }
        return Result.fail("删除异常");
    }

    //飞机信息增改接口
    @ResponseBody
    @GetMapping("/addPlaneMessage")
    public Result addPlaneMessage(@RequestParam(value = "planeId") String planeId,
                                  @RequestParam(value = "origin") String origin,//出发地
                                  @RequestParam(value = "destination") String destination,//目的地
                                  @RequestParam(value = "startTime") String startTime,//出发时间
                                  @RequestParam(value = "arriveTime") String arriveTime,//到达时间
                                  @RequestParam(value = "seats") int seats,//座位数
                                  @RequestParam(value = "remainTicket") int remainTicket,//剩余票数
                                  @RequestParam(value = "ticketPrice") float ticketPrice,//票价
                                  @RequestParam(value = "planeState") String planeState,
                                  @RequestParam(value = "planeRoute") String planeRoute){
        if (planeMessageService.findByPlaneId(planeId)!=null){
            return Result.fail("该航班号存在，添加失败");
        }
        PlaneMessage planeMessage = planeMessageService.getPlaneMessage(planeId, origin, destination, startTime, arriveTime, seats, remainTicket, ticketPrice, planeState,planeRoute);

        if (planeMessage.getStartTime().isAfter(planeMessage.getArriveTime())){
            return Result.fail("到达时间不能在出发时间前");
        }

        if (planeMessageService.editPlaneMessage(planeMessage)){
            return Result.ok();
        }
        return Result.fail("添加失败");

    }

    @ResponseBody
    @GetMapping("/editPlaneMessage")
    public Result editPlaneMessage(@RequestParam(value = "planeId") String planeId,
                                   @RequestParam(value = "origin") String origin,//出发地
                                   @RequestParam(value = "destination") String destination,//目的地
                                   @RequestParam(value = "startTime") String startTime,//出发时间
                                   @RequestParam(value = "arriveTime") String arriveTime,//到达时间
                                   @RequestParam(value = "seats") int seats,//座位数
                                   @RequestParam(value = "remainTicket") int remainTicket,//剩余票数
                                   @RequestParam(value = "ticketPrice") float ticketPrice,//票价
                                   @RequestParam(value = "planeState") String planeState,
                                   @RequestParam(value = "planeRoute") String planeRoute){
        if (message == null){return Result.fail("请选择一条数据后进行编辑");}
//        System.out.println(startTime);
        PlaneMessage planeMessage = planeMessageService.getPlaneMessage(planeId, origin, destination, startTime, arriveTime, seats, remainTicket, ticketPrice, planeState,planeRoute);
        if (planeMessage.getStartTime().isAfter(planeMessage.getArriveTime())){return Result.fail("到达时间不能在出发时间前");}

        //JPA保存方法save是ID相同时进行替换，不同时进行新增
        if (!message.getPlaneId().equals(planeId)){
            if (planeMessageService.findByPlaneId(planeId)!=null){
                return Result.fail("该航班号存在，修改失败");
            }
            planeMessageService.delPlaneMessageById(message.getPlaneId());
        }
        planeMessageService.editPlaneMessage(planeMessage);
        message = null;
        return Result.ok();
    }

    @PostMapping("/saveEditMessage/{planeId}")
    public Result saveEditMessage(@PathVariable String planeId){
        PlaneMessage planeMessage = planeMessageService.findByPlaneId(planeId);
        message = planeMessage;
        return Result.ok();
    }
}
